//firefox
function firefoxTab(evt) {
    var t = evt.target;
    var ss = t.selectionStart;
    var se = t.selectionEnd;
	var tab = String.fromCharCode(9);
 
    // Tab key - insert tab expansion
    if (evt.keyCode == 9) {
        evt.preventDefault();
                
        // Special case of multi line selection
        if (ss != se && t.value.slice(ss,se).indexOf("\n") != -1) {
            // In case selection was not of entire lines (e.g. selection begins in the middle of a line)
            // we ought to tab at the beginning as well as at the start of every following line.
            var pre = t.value.slice(0,ss);
            var sel = t.value.slice(ss,se).replace(/\n/g,"\n"+tab);
            var post = t.value.slice(se,t.value.length);
            t.value = pre.concat(tab).concat(sel).concat(post);
                    
            t.selectionStart = ss + tab.length;
            t.selectionEnd = se + tab.length;
        }
        // "Normal" case (no selection or selection on one line only)
        else {
            t.value = t.value.slice(0,ss).concat(tab).concat(t.value.slice(ss,t.value.length));
            if (ss == se) {
                t.selectionStart = t.selectionEnd = ss + tab.length;
            }
            else {
                t.selectionStart = ss + tab.length;
                t.selectionEnd = se + tab.length;
            }
        }
    }
}

//opera
function operaTab(e)
{
	var evtobj=window.event? event : e;
	var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
	if (unicode==9)
	{
		var obj = document.getElementById('content');
		if(document.selection)
		{
			obj.selection = document.selection.createRange();
			obj.selection.text = String.fromCharCode(9);
		}
		else
		{
			var before, after;
			before = obj.value.substring(0, obj.selectionStart);
			after = obj.value.substring(obj.selectionEnd, obj.value.length);
 
			obj.value= String.concat(before, String.fromCharCode(9), after);
		}
		
		if(evtobj.stopPropagation) {
			evtobj.stopPropagation(); }
		else {
			evtobj.cancelBubble = true; }

		if(evtobj.preventDefault) {
			evtobj.preventDefault(); }
		else {
			evtobj.returnValue = false; }
	}
	return false;
}

function chooseFunction(e)
{
	if(navigator.userAgent.indexOf("Firefox") != -1)
	{
	   return firefoxTab(e);
	}
	else if(navigator.userAgent.indexOf("Opera") != -1)
	{
		return operaTab(e);
	}
	else
	{
		return;
	}
}

function tablistener(){document.getElementById('content').addEventListener('keypress', chooseFunction, true);}
window.onload=tablistener;